package iosr11.gbase.dao;

import iosr11.data.EventSeries;
import iosr11.data.Student;
import iosr11.data.StudentEventSeries;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import com.google.api.gbase.client.FeedURLFactory;
import com.google.api.gbase.client.GoogleBaseAttributesExtension;
import com.google.api.gbase.client.GoogleBaseEntry;
import com.google.api.gbase.client.GoogleBaseFeed;
import com.google.api.gbase.client.GoogleBaseQuery;
import com.google.gdata.data.PlainTextConstruct;

public class StudentEventSeriesDAOImpl extends 
	GenericDAOImpl<StudentEventSeries> implements StudentEventSeriesDAO{
	
	private static String TYPE = "Student event series";
	private static String STUDENT_REF ="student";
	private static String ESERIES_REF ="event series";
	private Logger log = Logger.getLogger("service-log");

	@Override
	public List<StudentEventSeries> getEventSeriesForStudent(Student entity) {
		GoogleBaseQuery query = new GoogleBaseQuery(FeedURLFactory.getDefault()
				.getItemsFeedURL());
		GoogleBaseFeed feed = DAOLocatorImpl.getInstance()
				.getGoogleBaseQueryResult(query, STUDENT_REF,
						entity.getId());
		if (feed == null) {
			log.info("brak wpisow w bazie odpowiadajacych uzytkownikowi: "+entity.getName());
			return null;
		}
		feed = DAOLocatorImpl.getInstance().selectByType(feed, TYPE);
		if (feed == null) {
			log.info("brak serii eventow odnoszacych sie do uzytkownika: "+entity.getName());
			return null;
		}
		List<StudentEventSeries> result = new ArrayList<StudentEventSeries>();
		for (GoogleBaseEntry entry : feed.getEntries()) {
			StudentEventSeries tmp = this.rewrite(entry);
			tmp.setStudent(entity);
			result.add(tmp);
		}
		log.info("znaleziono "+result.size()+" event serii dla studenta");
		return result;
	}

	@Override
	public List<StudentEventSeries> getStudentsForEventSeries(EventSeries entity) {
		GoogleBaseQuery query = new GoogleBaseQuery(FeedURLFactory.getDefault()
				.getItemsFeedURL());
		GoogleBaseFeed feed = DAOLocatorImpl.getInstance()
				.getGoogleBaseQueryResult(query, ESERIES_REF,
						entity.getId());
		if (feed == null) {
			log.info("brak wpisow w bazie odpowiadajacych serii: "+entity.getId());
			return null;
		}
		feed = DAOLocatorImpl.getInstance().selectByType(feed, TYPE);
		if (feed == null) {
			log.info("brak serii eventow odnoszacych sie do serii: "+entity.getId());
			return null;
		}
		List<StudentEventSeries> result = new ArrayList<StudentEventSeries>();
		for (GoogleBaseEntry entry : feed.getEntries()) {
			StudentEventSeries tmp = this.rewrite(entry);
			tmp.setSeries(entity);
			result.add(tmp);
		}
		log.info("znaleziono "+result.size()+" studentow dla event serii");
		return result;
	}

	@Override
	public boolean checkType(GoogleBaseEntry entry) {
		return entry.getGoogleBaseAttributes().getItemType().equals(TYPE);
	}

	@Override
	public StudentEventSeries rewrite(GoogleBaseEntry entry) {
		Student student = new Student();
		EventSeries series = new EventSeries();
		student.setId(entry.getGoogleBaseAttributes().getTextAttribute(STUDENT_REF));
		series.setId(entry.getGoogleBaseAttributes().getTextAttribute(ESERIES_REF));
		StudentEventSeries tmp = new StudentEventSeries(student, series);
		tmp.setId(entry.getId());
		return tmp;
	}

	@Override
	public GoogleBaseEntry rewrite(StudentEventSeries entity) {
		GoogleBaseEntry entry = new GoogleBaseEntry();
		entry.setTitle(new PlainTextConstruct(TYPE));
		entry.setDraft(true);
		GoogleBaseAttributesExtension ext = entry.getGoogleBaseAttributes();
		ext.setItemType(TYPE);
		ext.addTextAttribute(STUDENT_REF, entity.getStudent().getId());
		ext.addTextAttribute(ESERIES_REF, entity.getSeries().getId());
		return entry;
	}

}
